package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.entity.CardCode;
import com.ruoyi.common.entity.dto.CardCodeDto;
import com.ruoyi.common.service.ICardCodeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 卡密Controller
 * 
 * @author ruoyi
 * @date 2025-08-17
 */
@RestController
@RequestMapping("/cardcode/cardcode")
public class CardCodeController extends BaseController
{
    @Autowired
    private ICardCodeService cardCodeService;

    /**
     * 查询卡密列表
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(CardCode cardCode)
    {
        startPage();
        List<CardCode> list = cardCodeService.selectCardCodeList(cardCode);
        return getDataTable(list);
    }

    /**
     * 导出卡密列表
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:export')")
    @Log(title = "卡密", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CardCode cardCode)
    {
        List<CardCode> list = cardCodeService.selectCardCodeList(cardCode);
        ExcelUtil<CardCode> util = new ExcelUtil<CardCode>(CardCode.class);
        util.exportExcel(response, list, "卡密数据");
    }

    /**
     * 获取卡密详细信息
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cardCodeService.selectCardCodeById(id));
    }

    /**
     * 新增卡密
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:add')")
    @Log(title = "卡密", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CardCode cardCode)
    {
        return toAjax(cardCodeService.insertCardCode(cardCode));
    }

    /**
     * 修改卡密
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:edit')")
    @Log(title = "卡密", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CardCode cardCode)
    {
        return toAjax(cardCodeService.updateCardCode(cardCode));
    }

    /**
     * 删除卡密
     */
    @PreAuthorize("@ss.hasPermi('cardcode:cardcode:remove')")
    @Log(title = "卡密", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cardCodeService.deleteCardCodeByIds(ids));
    }

    @PostMapping("/genercode")
    public AjaxResult genercode()
    {
        return cardCodeService.getCode();
    }

  /*  @GetMapping("/genercode")
    public AjaxResult getCode()
    {
        return cardCodeService.getCode();
    }*/

    @PostMapping("/checkcode")
    public R checkcode(@Valid @RequestBody CardCodeDto cardCode)
    {

        return cardCodeService.checkcode(cardCode);
    }
}
